import React, { useState } from 'react'
import { Button, message } from 'antd'
import '../index.less'
import { reqAddStuInfo } from '@/api'
import ModalForm from '@/components/ModalForm/ModalForm'

function CreateStu(props: any) {
  const [showForm,setShowForm] = useState(false)
  // 控制弹窗显隐
  const ctrlForm = () => {
    setShowForm(!showForm);
  };
  // 添加表格数据
  const createStu = (values:any) => {
    reqAddStuInfo(values).then(res => {
      const {code, msg} = res.data
      if(code === 200) {
        if(msg === '添加成功'){
          message.success(msg)
        }else {
          message.error(msg)
        }
      }
    })
    props.handleFlag() // 改变依赖更新父组件
    setShowForm(!showForm)    
  }
  return (
    <>
      <ModalForm
        showForm = {showForm}
        ctrlForm = {ctrlForm}
        formTitle = {'创建学生'}
        changeStu = {createStu}
        stuName = {'stuName'}
        stuID = {'stuID'}
        stuPwd = {'stuPwd'}
        stuAccount = {'stuAccount'}
        stuSchool = {'school'}
        stuClass = {'stuClass'}
        stuAddress = {'address'}
        stuPhone = {'phone'}
        stuNameInit = {''}
        stuIDInit = {''}
        stuPwdInit = {''}
        stuAccountInit = {''}
        stuSchoolInit = {''}
        stuClassInit = {''}
        stuAddressInit = {''}
        stuPhoneInit = {''}
        disabled = {false}
      />
      <Button onClick={ctrlForm} className='createBtn' >创建学生</Button>
    </>
  )
}

export default CreateStu
